package cn.saltybe.analysis.controller.api.statistic;

import cn.saltybe.analysis.entity.result.RentingHouseAveragePrice;
import cn.saltybe.analysis.service.RentingHouseAveragePriceStatisticServiceImpl;
import cn.saltybe.analysis.util.ApiResponse;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api/statistic/share-renting-house-average-price")
public class ShareRentingHouseAveragePriceAPI {
    private RentingHouseAveragePriceStatisticServiceImpl rentingHouseAveragePriceStatisticService;

    @Autowired
    public void setRentingHouseAveragePriceStatisticService(RentingHouseAveragePriceStatisticServiceImpl rentingHouseAveragePriceStatisticService) {
        this.rentingHouseAveragePriceStatisticService = rentingHouseAveragePriceStatisticService;
    }

    @ResponseBody
    @GetMapping(value = "list")
    public String list(
            @RequestParam(value = "city", required = false, defaultValue = "") String city
    ) throws Exception {
        List<RentingHouseAveragePrice> list;
        if (city != null && city.length() > 0) list = rentingHouseAveragePriceStatisticService.query("share", city);
        else list = rentingHouseAveragePriceStatisticService.all("share");
        JsonArray result = new Gson().toJsonTree(list).getAsJsonArray();
        return ApiResponse.buildSuccessResponse(result).toString();
    }
}
